package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.stereotype.Component;

@Component
@Data
public class AlipayConfigTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000136682704";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCPnmb3GsUoXxxHABhMXfs3hx0X7jB4QWnoUeZ2pAo3eVPJE/i8n2q6PPSy2ixnk5eGLG8rwLcH8pqAvk7mH2/0Fe63RyFcn2wnYD7/URYMgOLT5/86aCrkVo7A0xVS67nF1IPNOUOdW4y+MDqajFXWm1tqDAB6FSk30yUQBqQ/HAfSltS66gA3m4e9Gc6aJQYSQZpRVzE1r6VMzBj+H1p2jT9qLE1LcVW0pXxAGeIM5f0xj/KI9oKD4N7ooYRNo7mw940SsYJNdhg6m3KknvasYngtHRrWLzwflFaFgE2vb13ut/YZd988P8ZjJXjt+ZHC+o0ub2xt8ecQwsLqUHMJAgMBAAECggEAd+RuZF6OtDMh8gucPTQ/oY1DtlR7VgiVlwG3+NbcSI8aaMQpY51ZhXuZcy064ISPe8zgSZApu5LFdDBqwSUBWqxsicfxi9byfLSWIqHYXDvEH86OfH21QMUZitMr+/sZLJNKLCpfzZ89kQ/F/Hqmte8fkxnN1BySOi4jxIsm1h8WBad+vUw0PD+g6kGT8Rr+jLJCgT8qVXUZi+0VA7SiGev5ESibUTOikXQiIAa233HpueoyoJnvS4UjrMqPGa67NOMHLb5vf9LR6ObF8ttPokLMDFqkgAr3gwd2YEzu0NBdo77Iw/F6lBB6LErspbnxK4viZTVrjA9aQ/V10SSmOQKBgQDhNgdu4VebejeGyoditkfYm0K9Frh+vjkIksUMIyQLg67BPfqFw3nngcNQndjB8zTxtP1ber36XgdYkGWLs5zFFqyQ3Sv1kXN28ECM+dkmQ0Is+Ro9fOLmKdeVP4D76OqMJJrqqtTNE0xKC0eC4toHI/DPaUjEJOZYgzG9xHRU/wKBgQCjQMsosrrckCcQw4sjwIgQch20NpjqY8ac3yF7v8mnR8chLC1g9EbPTmASSljAUOcVmDL2iTKIlxp8RnrvZrTP9PTSX41eGaFs2z+fZ9qf/fq6QRwZsFWJhmByNufJf8XbxkF76GYBNGHW2rryYZoMbzrwWOvrl/hQe1GeJWiP9wKBgEVNQfZN446VilaxJf4rR2qRRHU5c6aU1KoduAMnISVl7TXL47ab03x5Auhg2l28gHNMlZY77BAKNUp8jLLzn29KremaFbfofVaBUevPypGqqsd6FCgNsSjQgWWM+B0MrHhZn05NKVS99oDmfaf6NMtztNuuc1TgYf2kyX6y4HVzAoGBAIfoyTl4klhNKjiqkouRHbWTS2dcjLGVnyO6HwYHBLpe50iWnrC4rLqpZRHVE04IDFnN+5+DWK6eHU6Y3n+v4Ze/NpJtOiO88sbNQIpUy42mw7WLvvkj9BIp7OEpFI6L8QymYJ3nPASEt29HaZuf5b+0Wjz9ymq4eeGGyMwAGKzBAoGBAIczW1OqhFXwwHd4Zu7nqxwgZzN+2Uzz+d9f467LVwbeE/5mdqYn6PnNlnEdb/TM4jkZC+11yTT1wSbVO4k8HdHdcen9eG2JRI9E+aCvGMITyP3KKGlS20V4z8YIwj+xPUyLnjDiCpjB3Ta7oqbjJGhdQtpnNVAcWquhmmPepRF2";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs2ctol5u1JkiDXJZiTJaV1VlzSDXEUeZP+DqdKoCnOBT7OjgFNRuyAuPcKqHEEgsr68lB7aE95X7cZ5KA3DGQhGL+6A54s3HkMZlIFLvYWxp1pdrtSql6nWg1nn2UpPy7wEUVOOO3T8EVrr3kvncsucas2FFI5vtWDtRbQyUPpTS5qSdJ/FQqgXhUeEWIMSf93iWuRN5mimhQYJUEABWBfn8v55oXLqMPKT3Nx5MnhXyGXiLetOGk2hcBgDn0iSxrZVc2nt7+Un7Wz9J/s/ZUwoAII+pSEloSPXEE4eH4K+YzpT5EvCxAHgDd8TOD4gt2w6DJyzyxnJUPatkgVlZ4wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://localhost:9060/memberOrder";

    // 签名方式
    private  String sign_type = "RSA2";

    //订单超时时间
    private String timeout="1m";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }

}
